//
//  ZWHChatViewController.swift
//  约麻
//
//  Created by Syrena on 2017/7/20.
//  Copyright © 2017年 Sweet. All rights reserved.
//

import UIKit
import EaseUI
import AVOSCloud

class ZWHChatViewController: EaseMessageViewController {
    
    var user:AVUser?

    override func viewDidLoad() {
        super.viewDidLoad()
        setnavigation()
        self.dataSource = self
        self.delegate = self
        if let name = user?.object(forKey: "name") as? String{
            title = name
        }else{
            title = user?.username
        }
        
        
    }
    //设置导航栏
    func setnavigation(){
        navigationController?.navigationBar.barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //定义标题颜色与字体大小字典
        let dict:NSDictionary = [NSForegroundColorAttributeName: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),NSFontAttributeName : UIFont.boldSystemFont(ofSize: 13)]
        //标题设置颜色与字体大小
        
        navigationController?.navigationBar.titleTextAttributes = dict as? [String : AnyObject]
        
        let Btn = UIButton(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
        Btn.setImage(UIImage.init(named:"黑色返回"), for: .normal)
        Btn.addTarget(self, action: #selector(backClicked), for: .touchUpInside)
        let back = UIBarButtonItem(customView: Btn)
        navigationItem.leftBarButtonItem = back
        
        
    }
    func backClicked(){
        navigationController?.popViewController(animated: true)
    }

}

extension ZWHChatViewController:EaseMessageViewControllerDelegate,EaseMessageViewControllerDataSource{
    
    func messageViewController(_ viewController: EaseMessageViewController!, modelFor message: EMMessage!) -> IMessageModel! {
        let model = EaseMessageModel(message: message)
        if model?.isSender == true{
            if let name = AVUser.current()?.object(forKey: "name") as? String{
                model?.nickname = name
            }else{
                model?.nickname = AVUser.current()?.username
            }
            
            if let image = AVUser.current()?.object(forKey: "headimage") as? String{
                model?.avatarURLPath = image
            }else{
                model?.avatarImage = UIImage(named: "茶叶")
            }
        }else{
            if let name = user?.object(forKey: "name") as? String{
                model?.nickname = name
            }else{
                model?.nickname = user?.username
            }
            
            if let image = user?.object(forKey: "headimage") as? String{
                model?.avatarURLPath = image
            }else{
                model?.avatarImage = UIImage(named: "茶叶")
            }
        }
        return model
    }
}
